package jlib.component.scroll
{
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import jlib.core.geom.Vector2D;
	import jlib.utils.ArrangeUtil;
	import jlib.utils.DepthUtil;
	

	public class ScrollBarVerticalState extends ScrollTrackManager implements IScrollBarDirectionState
	{
		private var m_size:Number;
		
		public function ScrollBarVerticalState($scrollbar:ScrollBar): void
		{
			super($scrollbar);
		}

		public function changeThumbScale(): void
		{	
			var totalOffset:Number = m_scrollbar.getThumbStartOffset()+m_scrollbar.getThumbEndOffset();
			var range:Number = m_scrollbar.getRange();
			var scalable:Boolean = m_scrollbar.getThumbScalable();
			
			if(!m_thumb || !m_track || !m_target) return;
			if(m_target.height/range == 0) return;
			if(scalable) m_thumb.height = m_track.height*(1/(m_target.height/range))-totalOffset;
			else m_thumb.scaleY = 1;
		}
		
		public function changeTargetPosition(): void
		{
			var range:Number = m_scrollbar.getRange();
			var guide:Number = m_scrollbar.getGuide();
			if(!m_target) return;
			
			var bound:Rectangle = m_target.getBounds(m_target);
			m_target.y = guide - (bound.y*m_target.scaleY + (bound.height*m_target.scaleY-range)*getPosition());
		}
		
		public function hasScrollBar(): Boolean 
		{ 
			var range:Number = m_scrollbar.getRange();
			if(!m_target) return false;			
			return ((m_target.height/range) > 1); 
		}
		
		public function getTrackSize(): Number { return m_size; }
		public function setTrackSize(sizePixel:Number): void 
		{ 
			m_size = sizePixel;
			m_scrollbar.getTrack().height = m_size; 
		}
		
		override protected function onThumbMove(e:Event):void
		{
			if(m_isDisposed) dispose();
			setPosition(m_thumbDownDistance+(m_stage.mouseY-m_downPosition.y)/m_movableLine.getDistance());
		}
		
		override protected function onTrackClick(e:Event):void 
		{
			if(m_isDisposed) dispose();
			if(e.target != m_track) return ;
			var totalOffset:Number = m_scrollbar.getThumbStartOffset()+m_scrollbar.getThumbEndOffset();
			setPosition((((m_track.mouseY-totalOffset)*m_track.scaleY)-m_thumb.height*0.5)/m_movableLine.getDistance()); 
		}
		
		override protected function updateMovableLine():void {
			var hdiff0:Number = (m_track.getBounds(m_track).width - m_thumb.getBounds(m_thumb).width)*0.5;
			var hdiff1:Number = (m_track.getRect(m_track).width - m_thumb.getRect(m_thumb).width)*0.5;
			var thickness:Number = hdiff1-hdiff0;
			
			var start:Point = m_movableLine.getStart();
			var end:Point = m_movableLine.getEnd();
			start.x = end.x = m_track.x+hdiff0+thickness;
			start.y = m_track.y+m_scrollbar.getThumbStartOffset();
			end.y = m_track.y+(m_track.height-m_thumb.height)+m_scrollbar.getThumbEndOffset();
		}
		
		override protected function updateCurrentPosition(): void {
			var totalOffset:Number = m_scrollbar.getThumbStartOffset()+m_scrollbar.getThumbEndOffset();
			m_currentPosition = ((m_thumb.y-m_track.y)-totalOffset)/m_movableLine.getDistance();
		}
		
	}//c
}